package com.alexsh.service.impl;

import com.alexsh.common.ApplicationContextHolder;
import com.alexsh.dao.BookDAO;
import com.alexsh.dao.impl.BooksRepository;
import com.alexsh.domain.Book;
import com.alexsh.service.BookManager;
import com.alexsh.service.bookspresentation.BookFactory;
import com.alexsh.service.bookspresentation.BookType;
import com.alexsh.service.bookspresentation.impl.BookContentGenerator;
import com.alexsh.service.bookspresentation.impl.BookInJSON;
import com.alexsh.service.bookspresentation.impl.BookInXML;
import com.alexsh.web.form.BookFormBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author Alexander Shmygin <AShmygin@luxoft.com>
 * @version 1.0
 */
public class BookManagerImpl implements BookManager {

    private static final long serialVersionUID = 4246785519716329088L;

    private static final Log logger = LogFactory.getLog(BookManagerImpl.class);

    private BooksRepository bookRepository;

    private static final SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy");

    @Autowired
    BookDAO bookDao;

    public String getBookList(BookType bookType) {
        BookContentGenerator bcg;
        switch (bookType) {
            case XML:
                BookFactory xmlBook = new BookInXML();
                bcg = new BookContentGenerator(xmlBook);
                return bcg.getContent();
            case JSON:
                BookFactory jsonBook = new BookInJSON();
                bcg = new BookContentGenerator(jsonBook);
                return bcg.getContent();
            default:
                break;
        }
        return null;
    }

    public String getBookById(String bookId, BookType bookType) {
        BookContentGenerator bcg;
        switch (bookType) {
            case XML:
                BookFactory xmlBook = new BookInXML();
                bcg = new BookContentGenerator(xmlBook);
                return bcg.getById(bookId);
            case JSON:
                BookFactory jsonBook = new BookInJSON();
                bcg = new BookContentGenerator(jsonBook);
                return bcg.getById(bookId);
            default:
                break;
        }
        return null;
    }

    public void saveBook(BookFormBean formBean) {
        setBookRepository();
        addBookToRepository(formBean);
        addBookToDB(formBean);
    }

    public void deleteBookById(String bookId) {
        setBookRepository();
        bookRepository.deleteBookById(bookId);
    }

    private void addBookToRepository(BookFormBean formBean) {
        Book book = convertToBook(formBean);
        bookRepository.addBook(book);
    }

    private void addBookToDB(BookFormBean formBean) {
        Book book = convertToBook(formBean);
        bookDao.save(book);
    }

    private Book convertToBook(final BookFormBean formBean) {
        Book book = new Book();
        book.setId(formBean.getId());
        book.setAuthors(formBean.getAuthors());
        book.setTitle(formBean.getTitle());
        try {
            book.setPubDate(SDF.parse(formBean.getPubDate()));
        }
        catch (ParseException e) {
            logger.error("Can't parse date.", e);
        }
        return book;
    }

    public void setBookRepository() {
        if (bookRepository == null) {
            this.bookRepository = (BooksRepository) ApplicationContextHolder.getBean("bookRepository");
        }
    }
}
